<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/9/27
 * Time: 19:55
 */

namespace app\admin\controller;

use think\Db;

class Permission extends AdminController
{
//    权限分类
    // public function PermissionList()
    // {
    //     return view('permission/permission-list');
    // }
    


//    权限管理
    public function PermissionRule()
    {
        // 查询权限表
        $list = Db::table('zz_node')->order('zz_node_controlname','asc')->paginate(10);
        

        // 统计有多少权限
        $num = sizeof($list);


        return view('permission/permission-rule',[
            'list' => $list,
            'num' => $num
        ]);
    }

    // 修改权限信息 页面
    public function PermissionAdd($id)
    {
        // 根据权限id 查询该权限所有信息
        $list = Db::table('zz_node')->where('zz_node_id',$id)->find();

        return view('permission/permission-add',[
            'list' => $list
        ]);
    }

    // 修改权限 信息
    public function PermissionDoAdd()
    {
        $id = input('id');
        $name = input('name');
        $controlname = input('controlname');
        $modelname = input('modelname');

        $data = [];
        $data['zz_node_name'] = $name;
        $data['zz_node_controlname'] = $controlname;
        $data['zz_node_modelname'] = $modelname;

        Db::table('zz_node')->where('zz_node_id',$id)->update($data);

        $res = [];
        $res['status'] = 1;
        return json($res);
    }

    // 删除
    public function PermissionDel()
    {
        // 接收用户id
        $id = input('id');
        
        Db::table('zz_node')->where('zz_node_id',$id)->delete();
        Db::table('zz_role_node')->where('zz_node_id',$id)->delete();

        $res = [];
        $res['status'] = 1;
        return json($res);
    }
    
    // 新增
    public function PermissionAddTo()
    {
        return view('permission/permission-addto');
    }

    public function PermissionDoAddTo()
    {
        $name = input('name');
        $controlname = input('controlname');
        $modelname = input('modelname');

        $data = [];
        $data['zz_node_name'] = $name;
        $data['zz_node_controlname'] = $controlname;
        $data['zz_node_modelname'] = $modelname;

        Db::table('zz_node')->insert($data);

        $res = [];
        $res['status'] = 1;
        return json($res);
    }

}